package org.jeecg.modules.farming.news.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.farming.news.entity.News;
import org.jeecg.modules.farming.news.entity.NewsComment;
import org.jeecg.modules.farming.news.entity.NewsPraise;
import org.jeecg.modules.farming.news.service.INewsPraiseService;
import org.jeecg.modules.farming.news.service.INewsService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.system.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
* @Description: 新闻咨询
* @Author: jeecg-boot
* @Date:   2020-12-11
* @Version: V1.0
*/
@Api(tags="新闻咨询")
@RestController
@RequestMapping("/min/news")
@Slf4j
public class MinNewsController extends JeecgController<News, INewsService> {
   @Autowired
   private INewsService newsService;
    @Autowired
    private INewsPraiseService praiseService;
    @Autowired
    private ISysUserService userService;
    @Value("${wx.fullUrl}")
    private String fullUrl;
    /**
     * 分页列表查询
     *
     * @param news
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "新闻咨询-分页列表查询")
    @ApiOperation(value="新闻咨询-分页列表查询", notes="新闻咨询-分页列表查询")
    @GetMapping(value = "/min/list")
    public Result<?> queryPageMinList(News news,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<News> queryWrapper = QueryGenerator.initQueryWrapper(news, req.getParameterMap());
        Page<News> page = new Page<News>(pageNo, pageSize);
        IPage<News> pageList = newsService.page(page, queryWrapper);
        List<News> records = pageList.getRecords();
        if(records !=null && records.size()>0){
            for(int i =0;i<records.size();i++){
                News n = records.get(i);
                if(n.getAttachment() != null)
                {
                    String[] split = n.getAttachment().split(",");
                    String strUrl = "";
                    for(int j=0;j<split.length;j++)
                    {
                        String url = split[j];
                        strUrl = SecurityUtil.GetFullUrl(url,fullUrl) + ",";
                    }
                    if(strUrl.length()>1)
                    {
                        strUrl = strUrl.substring(0,strUrl.length() - 1);
                    }
                    records.get(i).setAttachment(strUrl);
                }
                if(n.getImgUrl() !=null){
                    String[] split = n.getImgUrl().split(",");
                    String strUrl = "";
                    for(int j=0;j<split.length;j++)
                    {
                        String url = split[j];
                        strUrl = SecurityUtil.GetFullUrl(url,fullUrl) + ",";
                    }
                    if(strUrl.length()>1)
                    {
                        strUrl = strUrl.substring(0,strUrl.length() - 1);
                    }
                    records.get(i).setImgUrl(strUrl);
                }
            }
            pageList.setRecords(records);
        }
        return Result.OK(pageList);
    }
   /**
    *   添加
    *
    * @param news
    * @return
    */
   @AutoLog(value = "新闻咨询-添加")
   @ApiOperation(value="新闻咨询-添加", notes="新闻咨询-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody News news) {
       SysUser user = userService.getById(news.getUnionId());
       news.setPraiseCount(0);
       news.setClickCount(0);
       news.setUnionName(user.getUnionName());
       news.setIsPraise(0);
       news.setStatus(1);  //审核中
       news.setCreateBy(user.getUsername());
//       news.setImgUrl(news.getImgUrl().replace("http://tmp/","").replace("/tmp/",""));
//       news.setAttachment(news.getAttachment().replace("http://tmp/","").replace("/tmp/",""));
       newsService.save(news);
       return Result.OK("添加成功！");
   }

   /**
    *  编辑
    *
    * @param news
    * @return
    */
   @AutoLog(value = "新闻咨询-编辑")
   @ApiOperation(value="新闻咨询-编辑", notes="新闻咨询-编辑")
   @PostMapping(value = "/edit")
   public Result<?> edit(@RequestBody News news) {
       newsService.updateById(news);
       return Result.OK("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "新闻咨询-通过id删除")
   @ApiOperation(value="新闻咨询-通过id删除", notes="新闻咨询-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       newsService.removeById(id);
       return Result.OK("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "新闻咨询-批量删除")
   @ApiOperation(value="新闻咨询-批量删除", notes="新闻咨询-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.newsService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.OK("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @return
    */
   @AutoLog(value = "新闻咨询-通过id查询")
   @ApiOperation(value="新闻咨询-通过id查询", notes="新闻咨询-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="newsId",required=true) String newsId,@RequestParam(name="openId",required=true) String openId) {
       News news = newsService.getById(newsId);
       NewsPraise praise = praiseService.getOneByOpenIdAndCommentId(openId,newsId);
       if(praise !=null){
           news.setIsPraise(1);
           if(news.getAttachment() != null)
           {
               String[] split = news.getAttachment().split(",");
               String strUrl = "";
               for(int j=0;j<split.length;j++)
               {
                   String url = split[j];
                   strUrl = SecurityUtil.GetFullUrl(url,fullUrl) + ",";
               }
               if(strUrl.length()>1)
               {
                   strUrl = strUrl.substring(0,strUrl.length() - 1);
               }
               news.setAttachment(strUrl);
           }
           if(news.getImgUrl() !=null){
               String[] split = news.getImgUrl().split(",");
               String strUrl = "";
               for(int j=0;j<split.length;j++)
               {
                   String url = split[j];
                   strUrl = SecurityUtil.GetFullUrl(url,fullUrl) + ",";
               }
               if(strUrl.length()>1)
               {
                   strUrl = strUrl.substring(0,strUrl.length() - 1);
               }
               news.setImgUrl(strUrl);
           }
       }
       if(news==null) {
           return Result.error("未找到对应数据");
       }
       return Result.OK(news);
   }
    /**
     *   添加
     *
     * @param news
     * @return
     */
    @AutoLog(value = "新闻咨询-评价表-更新阅读次数")
    @ApiOperation(value="新闻咨询-评价表-更新阅读次数", notes="新闻咨询-评价表-更新阅读次数")
    @PostMapping(value = "/updateClickCount")
    public Result<?> updateClickCount(@RequestBody News news) {
        news = newsService.getById(news.getId());
        news.setClickCount(news.getClickCount() == null ? 1 : news.getClickCount()+1);
        newsService.updateById(news);
        return Result.OK("添加成功！");
    }
   /**
   * 导出excel
   *
   * @param request
   * @param news
   */
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, News news) {
       return super.exportXls(request, news, News.class, "新闻咨询");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, News.class);
   }
    /**
     *  批量审核
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "新闻咨询-批量审核")
    @ApiOperation(value="新闻咨询-批量审核", notes="新闻咨询-批量审核")
    @DeleteMapping(value = "/auditBatch")
    public Result<?> auditBatch(@RequestParam(name="ids",required=true) String ids,@RequestParam(name="auditValue",required=true) int auditValue) {
        this.newsService.auditBatch(ids,auditValue);
        return Result.OK("批量审核成功!");
    }
}
